package ae.pcfc.etks.cld.to.web.controller.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.multipart.MultipartFile;

import ae.pcfc.etks.cld.service.common.vo.AttachmentType;
import ae.pcfc.etks.cld.service.common.vo.FileVO;
import ae.pcfc.etks.cld.service.common.vo.UserVO;
import ae.pcfc.etks.cld.to.web.model.FilesIncomingBean;
import ae.pcfc.etks.cld.to.web.model.RequestVariablesBean;
import ae.pcfc.etks.cld.to.web.util.SecurityService;
import ae.pcfc.etks.cld.to.web.util.StringUtils;

/**
 * Base Controller for all controllers
 * @author 
 *
 */

public abstract class BaseController {
	
	//@Autowired
	protected SecurityService securityService;
	
	//@Autowired
	//@Qualifier("mapper")
	protected DozerBeanMapper mapper;
	
	//@Autowired(required=false)
	//@Qualifier("requestVariablesBean")
	protected RequestVariablesBean requestVariablesBean;
	
	//@Value("${work.mode}")
	private String mode;
	
	//@Value("${login.context.path}")
	private String loginPath;

	@Autowired
	protected MessageSource messageSource;
	
	protected DozerBeanMapper getMapper() {
		return mapper;
	}
	
	protected FileVO createFileVOFromMultipartFile(MultipartFile multipartFile, String typeCode) throws IOException{
		
		FileVO fileVO = new FileVO();
		fileVO.setAttachmentTypeCode(typeCode);
		fileVO.setInputStream(multipartFile.getInputStream());
		String fileOriginalName = multipartFile.getOriginalFilename();
		fileVO.setFileExt(StringUtils.getFileExtension(fileOriginalName));
		fileVO.setFileName(fileOriginalName);
		fileVO.setUploadDate(new Date());
		return fileVO;
	}
	
	protected List<FileVO> getFileVOList(FilesIncomingBean filesIncomingBean) throws IOException{
		MultipartFile passportCopy = filesIncomingBean.getPassportCopy() ;
		MultipartFile letterFromCompany = filesIncomingBean.getLetterFromCompany();
		MultipartFile photograph = filesIncomingBean.getPhotograph();
		
		MultipartFile insurancePaper = filesIncomingBean.getInsurancePaper();
		MultipartFile cancellationOrVisitCopy = filesIncomingBean.getCancellationOrVisitCopy();
		MultipartFile cancellationPaper = filesIncomingBean.getCancellationPaper();
		
		MultipartFile exitDocument = filesIncomingBean.getExitDocument();
		
		List<FileVO> fileVOList = new ArrayList<FileVO>();
		
		if(passportCopy!=null && !passportCopy.isEmpty()){
			fileVOList.add(createFileVOFromMultipartFile(passportCopy, AttachmentType.__ATTACHMENT_TYPE_PASSPORT));
		}
		if(letterFromCompany!=null && !letterFromCompany.isEmpty()){
			fileVOList.add(createFileVOFromMultipartFile(letterFromCompany, AttachmentType.__ATTACHMENT_TYPE_COMPANY_LETTER));
		}
		if(photograph!=null && !photograph.isEmpty()){
			fileVOList.add(createFileVOFromMultipartFile(photograph, AttachmentType.__ATTACHMENT_TYPE_PHOTO));
		}
		if(insurancePaper!=null && !insurancePaper.isEmpty()){
			fileVOList.add(createFileVOFromMultipartFile(insurancePaper, AttachmentType.__ATTACHMENT_TYPE_INSURRANCE_PAPER));
		}
		if(cancellationOrVisitCopy!=null && !cancellationOrVisitCopy.isEmpty()){
			fileVOList.add(createFileVOFromMultipartFile(cancellationOrVisitCopy, AttachmentType.__ATTACHMENT_TYPE_CANCELATION_OR_VISIT_PAPER));
		}
		if(cancellationPaper!=null && !cancellationPaper.isEmpty()){
			fileVOList.add(createFileVOFromMultipartFile(cancellationPaper, AttachmentType.__ATTACHMENT_TYPE_CANCELATION_PAPER));
		}
		if(exitDocument!=null && ! exitDocument.isEmpty()){
			fileVOList.add(createFileVOFromMultipartFile(exitDocument, AttachmentType.__ATTACHMENT_TYPE_EXIT_DOCUMENT));
		}
		
		return fileVOList;
	}
	
	protected UserVO getUserVO(){
		UserVO userVO = new UserVO();
		userVO.setUsername(securityService.getLoginedUsername());
		userVO.setEmployee(securityService.isCompanyEmployee());
		userVO.setAdmin(securityService.isCompanyAdmin());
		userVO.setGeneralAdmin(securityService.isGeneralAdmin());
		return userVO;
	}
	
	protected UserVO getGeneralUserVO(){
		UserVO userVO = new UserVO();
		userVO.setUsername("BackOfficeUser");
		userVO.setEmployee(false);
		userVO.setAdmin(false);
		userVO.setGeneralAdmin(true);
		return userVO;
	}
	
}
